package com.jk.dao;

import com.jk.pojo.PayLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AutoSplitJointPorvider {

    // 例子1
    /*public String select6(final Demo demo){
        return new SQL(){{
            SELECT("id,name,email");
            FROM("demo");
            if(demo.getName() != null){
                WHERE("name=#{name}");
            }
            if(demo.getEmail() != null){
                WHERE("email=#{email}");
            }
        }}.toString();
    }*/

    // 例子2
    /*public String select5(Demo demo){
        StringBuffer sql = new StringBuffer("select *from demo where 1=1 ");
        if(demo.getName() != null){
            sql.append(" and name=#{name}");
        }
        if(demo.getEmail() != null){
            sql.append(" and email=#{email}");
        }
        return sql.toString();
    }*/

    //分页查询交易
    public String queryLogList(PayLog payLog){
        StringBuffer str = new StringBuffer("select * from pay_log where 1 = 1 ");
        str.append(" and logStatus = 1 ");
        str.append(" and userId = #{userId} ");
        if (payLog.getSaveTime() != null){
            str.append(" and saveTime = #{saveTime} ");
        }
        if (payLog.getLogType() != null){
            str.append(" and logType = #{logType} ");
        }
        str.append(" ORDER BY saveTime DESC LIMIT #{startIndex},#{rows}");
        return str.toString();
    }
    //交易记录逻辑删除
    public String updateDelete(String logId){
        return new SQL() {
            {
                UPDATE("pay_log");
                SET("logStatus = 2");
                WHERE("logId = #{logId}");
            }
        }.toString();
    }
    //分页查询回收站
    public String queryRecycleList(PayLog payLog){
        StringBuffer str = new StringBuffer("select * from pay_log where 1 = 1 ");
        str.append(" and logStatus = 2 ");
        str.append(" and userId = #{userId} ");
        str.append(" ORDER BY saveTime DESC LIMIT #{startIndex},#{rows}");
        return str.toString();
    }
    //回收站物理批量删除
    public String deleteRecycleList(List<String> list){
        String ids= "";
        for (int i = 0; i < list.size(); i++) {
            ids+=","+list.get(i);
        }
        final  String strIds = ids.substring(1);
        return new SQL(){
            {
                DELETE_FROM("pay_log");
                WHERE("logId in ("+strIds+")");
            }
        }.toString();
    }
    //回收站数据批量还原
    public String updateReduction(List<String> list){
        String ids= "";
        for (int i = 0; i < list.size(); i++) {
            ids+=","+list.get(i);
        }
        final  String strIds = ids.substring(1);
        return new SQL(){
            {
                UPDATE("pay_log");
                SET("logStatus = 1");
                WHERE("logId in ("+strIds+")");
            }
        }.toString();
    }
    //账单查询总条数
    public  String  queryCount(){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("pay_log");
            }
        }.toString();
    }

    //账单查询详情数据
    public String queryPactList(int start, int end){
        return new SQL(){
            {
                SELECT("*");
                FROM("pay_log LIMIT #{start},#{end}");
            }
        }.toString();
    }
}
